<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>测试页</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet" href="/static/css/gdbase_m_core.css">
    <script src="https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
    <script src="/static/js/gdbase_m_core.js"></script>
</head>
<body>
<div class="page-group">
    <div class="page page-current">
        我是测试页，我来啦
        <a class="button button-link button-nav pull-left back" href="{:url('index')}">返回上一页</a>

        <input type="file" name="test">
        <div>
            我的uid：<span id="myuid">0</span>
        </div>
        <div>
            在线人数：<span id="zaixianrenshu">0</span>
        </div>
        <div>
            收到消息：
            <div>
                <ul id="shoudaoxiaoxi">
                </ul>
            </div>
        </div>
    </div>
    <script language='javascript'>
        var uid = new Date().getTime();
        $(function () {
            $('#myuid').html(uid);
        })
        var wsEvent = {
            'update_online_num':function (_data) {
                console.log(_data)
                $('#zaixianrenshu').html(_data['num']);
            }
            ,'send_my_msg':function (_data) {
                $('#shoudaoxiaoxi').append('<li>'+_data['msg']+'</li>')
            }
            ,'to_back':function (_data) {
                alert(_data['msg']);
            }
        }
        ws = new WebSocket("ws://gdbase.me:2346");
        ws.onopen = function() {
            console.log("连接成功");
            // 注册一个用户
            wsSendMsg('member/login',{
                uid:uid
            });
        };
        ws.onmessage = function(e) {
            console.log("收到服务端的消息：" + e.data);
            var json = JSON.parse(e.data);
            if(!wsEvent[json['event']]){
                console.log(json['event'] + ' 事件未定义接收方法');
                return;
            }

            wsEvent[json['event']](json['data']);
        };
        // 断开 web socket 连接成功触发事件
        ws.onclose = function () {
            alert("连接已关闭...");
        };
        function wsSendMsg(_method, _params) {
            var json = {
                method:_method,
                params:_params,
            }
            ws.send(JSON.stringify(json));
        }
        function sendFile() {
            var file = document
                .querySelector('input[type="file"]')
                .files[0];
            ws.send(file);
        }
    </script>
</div>
</body>
</html>